package com.ali.resource.ServicePage;

import com.ali.resource.CompoentPage.EcsComponent;
import com.ali.resource.MapPage.ecs.EcsMap;
import com.ali.resource.ShareTools.BasicTools;
import com.ali.resource.ShareTools.TeaopenapiClient;

import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ssl.SslProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;




@Service
public class EcsService {


    @Autowired
    private EcsComponent ecsComponent;

    @Autowired
    private TeaopenapiClient teaopenapiClient;


    public void getExecl(ArrayList<EcsMap> ecsInstance) throws Exception {


        ArrayList<String> al = new ArrayList<>();
        al.add("instanceId");
        al.add("instanceName");
        al.add("cpu");
        al.add("mem");
        al.add("lll:deployment:environment");
        al.add("lll:business:application-name");
        al.add("lll:business:application-owner");
        al.add("lll:business:tech-owner");
        al.add("lll:business:vendor");
        al.add("Status");
        al.add("VpcId");
        al.add("VSwitchId");
        al.add("description");


        XSSFWorkbook wk = new XSSFWorkbook();
        XSSFSheet ecs = wk.createSheet("ecs");


        XSSFRow row = ecs.createRow(0);
        for (int i = 0; i < al.size(); i++) {
            row.createCell(i).setCellValue(al.get(i));
        }

        int ecsCount = 1 ;
        for (EcsMap ecsMap : ecsInstance) {
            XSSFRow rows = ecs.createRow(ecsCount);
            rows.createCell(0).setCellValue(ecsMap.getInstanceId());
            rows.createCell(1).setCellValue(ecsMap.getInstanceName());
            rows.createCell(2).setCellValue(ecsMap.getCpu());
            rows.createCell(3).setCellValue(ecsMap.getMem());
            rows.createCell(4).setCellValue(ecsMap.getEnvironment());
            rows.createCell(5).setCellValue(ecsMap.getApplicationName());
            rows.createCell(6).setCellValue(ecsMap.getApplicationOwner());
            rows.createCell(7).setCellValue(ecsMap.getTechOwner());
            rows.createCell(8).setCellValue(ecsMap.getVendor());
            rows.createCell(9).setCellValue(ecsMap.getInstanceStatus());
            rows.createCell(10).setCellValue(ecsMap.getInstanceVpcInfo());
            rows.createCell(11).setCellValue(ecsMap.getInstanceSwitchInfo());
            rows.createCell(12).setCellValue(ecsMap.getDescription());
            ecsCount += 1;
        }

        File file = BasicTools.setFIlePath("ecs.xlsx");

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        wk.write(fileOutputStream);
        fileOutputStream.close();
        wk.close();
    }

    public ArrayList<EcsMap> getList(@RequestParam(required = true,defaultValue = "false") String bool
            , @RequestParam(required = false) String instanceIdPrefix
            , @RequestParam(required = false) String instanceNamePrefix
            ) throws Exception {

        DescribeInstancesResponse dis = ecsComponent.getInstancesResource(ecsComponent.getRequest());
        int totalCount = dis.getBody().totalCount;
        int page = (totalCount / 50);
        if (0 != (totalCount % 50)){
            page += 1;
        }

        ArrayList<EcsMap> ecsInstance = new ArrayList<>();

        for (int i = 1; i < page + 1; i++) {
            DescribeInstancesRequest request = ecsComponent.getRequest();
            request.setPageSize(50);
            request.setPageNumber(i);
            DescribeInstancesResponse response = ecsComponent.getInstancesResource(request);

            List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instance = response.getBody().getInstances().getInstance();
            for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance ds : instance) {
                EcsMap ecs = this.getEcs(ds);

                if (instanceIdPrefix != null && instanceNamePrefix != null){

                    if (ds.instanceId.toLowerCase().contains(instanceIdPrefix.toLowerCase().trim()) && ds.instanceName.toLowerCase().contains(instanceNamePrefix.toLowerCase().trim())){
                        ecsInstance.add(ecs);
                    }

                } else if (instanceIdPrefix != null && instanceNamePrefix == null){

                    if (ds.instanceId.toLowerCase().contains(instanceIdPrefix.toLowerCase().trim())){
                        ecsInstance.add(ecs);
                    }

                } else if (instanceNamePrefix != null && instanceIdPrefix == null){

                    if (ds.instanceName.toLowerCase().contains(instanceNamePrefix.toLowerCase().trim())){
                        ecsInstance.add(ecs);
                    }

                } else if (instanceIdPrefix == null && instanceNamePrefix == null){
                    ecsInstance.add(ecs);
                }

            }
        }

            return ecsInstance;
    }

    public EcsMap getEcs(DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance ds){
        EcsMap ecsMap = new EcsMap();
        ecsMap.setInstanceId(ds.getInstanceId());
        ecsMap.setInstanceName(ds.getInstanceName());
        ecsMap.setCpu(ds.getCpu().toString());
        ecsMap.setMem(ds.getMemory().toString());
        if (ds.getTags() != null){
            List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstanceTagsTag> tag = ds.getTags().getTag();
            for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstanceTagsTag ts : tag) {
                if (ts.getTagKey().equals("lll:deployment:environment")){
                    ecsMap.setEnvironment(ts.getTagValue());
                }
                if (ts.getTagKey().equals("lll:business:application-name")) {
                    ecsMap.setApplicationName(ts.getTagValue());
                }
                if (ts.getTagKey().equals("lll:business:application-owner")) {
                    ecsMap.setApplicationOwner(ts.getTagValue());
                }
                if (ts.getTagKey().equals("lll:business:tech-owner")){
                    ecsMap.setTechOwner(ts.getTagValue());
                }
                if (ts.getTagKey().equals("lll:business:vendor")){
                    ecsMap.setVendor(ts.getTagValue());
                }
            }
        }
        ecsMap.setInstanceStatus(ds.getStatus());
        ecsMap.setInstanceVpcInfo(ds.getVpcAttributes().getVpcId());
        ecsMap.setInstanceSwitchInfo(ds.getVpcAttributes().getVSwitchId());
        ecsMap.setDescription(ds.getDescription());

        return ecsMap;
    }

}

